/*
 *	(*^。^*) TFrameWork License (*^。^*)
 */

/*
 *  Title :
 *
 *  Description :
 *
 *
 *  Date :
 *
 *  Author : DengPing
 *
 *  Modify :
 */
using Unity.VisualScripting;
using UnityEngine;

namespace TFrameWork.Module.ActionEditor
{
    public class CustomOnAnimationTrack : IOnAnimationTrack
    {
        public TrackEventBase eventData { get; private set; }
        public TimeLineAction timeLineAction { get; private set; }
        public int currentFrame { get; set; }
        private AnimationController animator;

        private CharacterController _characterController;

        public void Init(TimeLineAction timeLineAction)
        {
            this.timeLineAction = timeLineAction;
            animator = timeLineAction.GetComponent<AnimationController>();
            _characterController = timeLineAction.GetComponent<CharacterController>();
            if (animator == null)
            {
                animator = timeLineAction.AddComponent<AnimationController>();
                animator.Init();
            }
        }

        public void OnStart(TrackEventBase data)
        {
            eventData = data;

            eventData = data;
            var animationEvent = data as AnimationEvent;
            Debug.Log("Playanimation");
            animator.PlaySingleAniamtion(animationEvent.animationClip, 1, true, animationEvent.TransitionTime);

            if (animationEvent.applyRootMotion)
            {
                animator.SetRootMotionAction((deltaPosition, deltaRotation) =>
                {
                    _characterController.Move(deltaPosition);
                    _characterController.transform.rotation *= deltaRotation;
                });
            }
            else
            {
                animator.ClearRootMotionAction();
            }
        }

        public void OnUpdate(int frameIndex)
        {
            currentFrame = frameIndex;
        }

        public void OnFinish()
        {

        }

        public void OnBreak(BreakType type)
        {

        }
    }

}